package com.hundsun.yr.universal.p2p.ui.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.CompoundButton;

import com.hundsun.mobile.zwgt.R;
import com.hundsun.yr.universal.library.eventbus.EventCenter;
import com.hundsun.yr.universal.library.netstatus.NetUtils;
import com.hundsun.yr.universal.library.widget.HSButton;
import com.hundsun.yr.universal.library.widget.SwitchButton;
import com.hundsun.yr.universal.p2p.bean.MessageSettingBean;
import com.hundsun.yr.universal.p2p.bean.UserInfo;
import com.hundsun.yr.universal.p2p.config.Constants;
import com.hundsun.yr.universal.p2p.config.TaskConstants;
import com.hundsun.yr.universal.p2p.presenter.base.RequestPresenter;
import com.hundsun.yr.universal.p2p.presenter.impl.InitializePresenterImpl;
import com.hundsun.yr.universal.p2p.ui.activity.base.BaseActivity;
import com.hundsun.yr.universal.p2p.utils.BaseRequestTool2;
import com.hundsun.yr.universal.p2p.utils.CommTool;
import com.hundsun.yr.universal.p2p.utils.Logs;
import com.hundsun.yr.universal.p2p.view.base.InitializeView;

import org.json.JSONObject;

import java.util.HashMap;

/**
 * Created by apple on 2017/12/5.
 */

public class MessageSettingActivity extends BaseActivity implements InitializeView,CompoundButton.OnCheckedChangeListener,View.OnClickListener {
    private SwitchButton switchb_product_message = null;
    private SwitchButton switchb_activity_message = null;
    private SwitchButton switchb_disocunt_message = null;
    private HSButton btn_message_setting_sure = null;
    private String zwgt_data;
    private RequestPresenter mPresenter = null;
    private String product = null;
    private String activity = null;
    private String sale = null;

    @Override
    protected boolean isApplyKitKatTranslucency() {
        return false;
    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.act_message_setting;
    }

    @Override
    protected void initViewsAndEvents() {
        setToolbarTitle(R.string.message_setting);
        findView();
        loadingdata();
    }

    private void findView(){
        switchb_product_message = findView(R.id.switchb_product_message);
        switchb_activity_message = findView(R.id.switchb_activity_message);
        switchb_disocunt_message = findView(R.id.switchb_disocunt_message);
        btn_message_setting_sure = findView(R.id.btn_message_setting_sure);

        switchb_product_message.setOnCheckedChangeListener(this);
        switchb_activity_message.setOnCheckedChangeListener(this);
        switchb_disocunt_message.setOnCheckedChangeListener(this);
        btn_message_setting_sure.setOnClickListener(this);
    }

    private void loadingdata() {
        mPresenter = new InitializePresenterImpl(mContext,this);
        zwgt_data = BaseRequestTool2.getFormatedDateString();
        JSONObject map = new JSONObject();
        try {
            map.put("a","1");//随便传的无意义的参数，不然不能完成加密
        } catch (Exception e) {
            e.printStackTrace();
        }

        JSONObject jsonParams = CommTool.getJSONParams(map, sharedPreferences.getShareProf(UserInfo.TOKENID).substring(0, 24), zwgt_data);
        mPresenter.loadingData(TaskConstants.TASK_NOTICELIST, getString(R.string.root_url_test) + getString(R.string.api_user_test) +
                TaskConstants.TASK_NOTICELIST, Constants.EVENT_REFRESH_DATA, jsonParams, BaseRequestTool2.getHearder(mContext,
                TaskConstants.TASK_POST, mContext.getString(R.string.api_user_test) + TaskConstants.TASK_NOTICELIST, sharedPreferences.getShareProf(UserInfo.USER_NO),
                sharedPreferences.getShareProf(UserInfo.TOKENID), zwgt_data), true);
    }

    protected void initImmersionBar() {
        super.initImmersionBar();
        mImmersionBar.titleBar(R.id.common_toolbar).init();
    }

    @Override
    protected void getBundleExtras(Bundle extras) {

    }

    @Override
    protected void onEveComming(EventCenter eventCenter) {

    }

    @Override
    protected boolean isBindEventBusHere() {
        return false;
    }

    @Override
    protected View getLoadingTargetView() {
        return null;
    }

    @Override
    protected void onNetworkConnected(NetUtils.NetType type) {

    }

    @Override
    protected void onNetworkDisConnected() {

    }

    @Override
    protected boolean isApplyStatusBarTranslucency() {
        return false;
    }

    @Override
    protected boolean toggleOverridePendingTransition() {
        return false;
    }

    @Override
    protected TransitionMode getOverridePendingTransitionMode() {
        return null;
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        switch (buttonView.getId()){
            case R.id.switchb_product_message:
                if (isChecked){
                    product = "0";
                } else {
                    product = "1";
                }
                break;
            case R.id.switchb_activity_message:
                if (isChecked){
                    activity = "0";
                } else {
                    activity = "1";
                }
                break;
            case R.id.switchb_disocunt_message:
                if (isChecked){
                    sale = "0";
                } else {
                    sale = "1";
                }
                break;

        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_message_setting_sure:
//                product = "0";
//                activity = "0";
//                sale = "0";
                if (product == null || activity == null || sale == null){
                    return;
                }

                zwgt_data = BaseRequestTool2.getFormatedDateString();
                JSONObject map = new JSONObject();
                try {
                    map.put("product", product);
                    map.put("activity", activity);
                    map.put("sale", sale);
                } catch (Exception e) {
                    e.printStackTrace();
                }

                JSONObject jsonParams = CommTool.getJSONParams(map, sharedPreferences.getShareProf(UserInfo.TOKENID).substring(0, 24), zwgt_data);


                mPresenter.loadingData(TaskConstants.TASK_NOTICESET, getString(R.string.root_url_test) + getString(R.string.api_user_test) +
                        TaskConstants.TASK_NOTICESET, Constants.EVENT_REFRESH_DATA, jsonParams, BaseRequestTool2.getHearder(mContext,
                        TaskConstants.TASK_POST, mContext.getString(R.string.api_user_test) + TaskConstants.TASK_NOTICESET, sharedPreferences.getShareProf(UserInfo.USER_NO),
                        sharedPreferences.getShareProf(UserInfo.TOKENID), zwgt_data), true);
                break;
        }
    }

    @Override
    public <T> void initializeViews(String task_name, T data) {
        switch (task_name){
            case TaskConstants.TASK_NOTICELIST:
                //获取3个消息设置状态
                MessageSettingBean messageSettingBean = (MessageSettingBean) data;
                product = messageSettingBean.product;
                activity = messageSettingBean.activity;
                sale = messageSettingBean.sale;
                if (product.equals("0")){
                    switchb_product_message.setChecked(true);
                } else {
                    switchb_product_message.setChecked(false);
                }

                if (activity.equals("0")){
                    switchb_activity_message.setChecked(true);
                } else {
                    switchb_activity_message.setChecked(false);
                }

                if (sale.equals("0")){
                    switchb_disocunt_message.setChecked(true);
                } else {
                    switchb_disocunt_message.setChecked(false);
                }

                break;
            case TaskConstants.TASK_NOTICESET:
                //修改消息设置
                finish();
                break;
        }
    }

    @Override
    public <T> void initializeError(String task_name, T data) {
        switch (task_name){
            case TaskConstants.TASK_NOTICELIST:
                //获取3个消息设置状态

                break;
            case TaskConstants.TASK_NOTICESET:
                //修改消息设置
                finish();
                break;
        }
    }
}
